package cn.iocoder.yudao.module.system.controller.admin.roleaccess;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.roleaccess.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.roleaccess.RoleAccessDO;
import cn.iocoder.yudao.module.system.service.roleaccess.RoleAccessService;

@Tag(name = "管理后台 - 角色和APIJSON权限关联")
@RestController
@RequestMapping("/system/role-access")
@Validated
public class RoleAccessController {

    @Resource
    private RoleAccessService roleAccessService;

    @PostMapping("/create")
    @Operation(summary = "创建角色和APIJSON权限关联")
    @PreAuthorize("@ss.hasPermission('system:role-access:create')")
    public CommonResult<Long> createRoleAccess(@Valid @RequestBody RoleAccessSaveReqVO createReqVO) {
        return success(roleAccessService.createRoleAccess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色和APIJSON权限关联")
    @PreAuthorize("@ss.hasPermission('system:role-access:update')")
    public CommonResult<Boolean> updateRoleAccess(@Valid @RequestBody RoleAccessSaveReqVO updateReqVO) {
        roleAccessService.updateRoleAccess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色和APIJSON权限关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:role-access:delete')")
    public CommonResult<Boolean> deleteRoleAccess(@RequestParam("id") Long id) {
        roleAccessService.deleteRoleAccess(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除角色和APIJSON权限关联")
    @PreAuthorize("@ss.hasPermission('system:role-access:delete')")
    public CommonResult<Boolean> deleteRoleAccessList(@RequestParam("ids") List<Long> ids) {
        roleAccessService.deleteRoleAccessListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色和APIJSON权限关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:role-access:query')")
    public CommonResult<RoleAccessRespVO> getRoleAccess(@RequestParam("id") Long id) {
        RoleAccessDO roleAccess = roleAccessService.getRoleAccess(id);
        return success(BeanUtils.toBean(roleAccess, RoleAccessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色和APIJSON权限关联分页")
    @PreAuthorize("@ss.hasPermission('system:role-access:query')")
    public CommonResult<PageResult<RoleAccessRespVO>> getRoleAccessPage(@Valid RoleAccessPageReqVO pageReqVO) {
        PageResult<RoleAccessDO> pageResult = roleAccessService.getRoleAccessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RoleAccessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出角色和APIJSON权限关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:role-access:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoleAccessExcel(@Valid RoleAccessPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RoleAccessDO> list = roleAccessService.getRoleAccessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "角色和APIJSON权限关联.xls", "数据", RoleAccessRespVO.class,
                BeanUtils.toBean(list, RoleAccessRespVO.class));
    }


    @GetMapping("/getRoleAccessListByRoleId")
    @Operation(summary = "获得角色的APIJSON权限")
    @PreAuthorize("@ss.hasPermission('system:role-access:getRoleAccessListByRoleId')")
    public CommonResult<List<RoleAccessDO>> getRoleAccessListByRoleId(@Valid RoleAccessRoleReqVO roleAccessRoleReqVO) {
        List<RoleAccessDO> roleAccessListByRoleId = roleAccessService.getRoleAccessListByRoleId(roleAccessRoleReqVO);
        return success(roleAccessListByRoleId);
    }

    @PostMapping("/addRoleAccess")
    @Operation(summary = "获得角色的APIJSON权限")
    @PreAuthorize("@ss.hasPermission('system:role-access:addRoleAccess')")
    public CommonResult<Boolean> addRoleAccess(@Valid @RequestBody RoleAccessAddReqVO roleAccessAddReqVO) {
        roleAccessService.addRoleAccess(roleAccessAddReqVO);
        return success(true);
    }
}